package com.ktjiaoyu.crm.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ktjiaoyu.crm.pojo.Right;
import com.ktjiaoyu.crm.pojo.Role;
import com.ktjiaoyu.crm.service.RightService;
import com.ktjiaoyu.crm.service.RoleService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/role")
public class RoleController {
    @Resource
    RoleService roleService;
    @Resource
    RightService rightService;

    @GetMapping("/findAllRoles")
    @ResponseBody
    public List<Role> findAllRoles(){
        return roleService.findAllRoles();
    }

    @RequestMapping("/list")
    public String list(@RequestParam(required = false) String roleName,
                       @RequestParam(required = false,defaultValue = "1") Integer pageIndex,
                       Model model){
        //分页查询
        Integer pageSize=3;
        IPage<Role> page = roleService.getRolesByPage(roleName, pageIndex, pageSize);

        model.addAttribute("rolePage",page);

        //过滤条件回显
        model.addAttribute("roleName",roleName);

        //返回逻辑视图
        return "role/list";
    }

    @GetMapping("/add")
    public String add(Model model){
        //查询出所有权限
        List<Right> list = rightService.findAllRights();
        model.addAttribute("rights",list);

        return "role/add";
    }

    @PostMapping("/save")
    public String save(Role role,String[] rightCodes){
        try {
            roleService.addRole(role,rightCodes);
            return "redirect:/role/list";
        } catch (Exception e) {
            e.printStackTrace();
            return "redirect:/role/add";
        }
    }

    @GetMapping("/edit")
    public String edit(Long roleId,Model model){
        //查询当前角色
        Role role = roleService.getRole(roleId);
        model.addAttribute("role",role);

        //查询出所有权限
        List<Right> list = rightService.findAllRights();
        model.addAttribute("rights",list);

        //查询角色所拥有的权限
        List<Right> rights = rightService.findRightsByRoleId(roleId);
        model.addAttribute("roleRights",rights);

        return "role/edit";
    }

    @PostMapping("/update")
    public String update(Role role,String[] rightCodes){
        try {
            roleService.updateRole(role,rightCodes);
            return "redirect:/role/list";
        } catch (Exception e) {
            e.printStackTrace();
            return "redirect:/role/edit?roleId="+role.getRoleId();
        }
    }

    @RequestMapping("/delete")
    @ResponseBody
    public Map<String,Object> delete(Long roleId){
        Map<String,Object> map=new HashMap<>();

        try {
            roleService.deleteRole(roleId);
            map.put("delResult",true);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("delResult",false);
        }

        return map;
    }

}
